﻿<?xml version="1.0" encoding="utf-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html>
<!-- saved from url=(0033)http://research.microsoft.com/pex -->
<head><link rel="StyleSheet" href="er.common.css" type="text/css" /><script src="er.common.js" type="text/javascript"> </script><link rel="SHORTCUT ICON" href="favicon.ico" /><link rel="icon" href="favicon.ico" type="image/x-icon" /><title>C:\Users\AO\Documents\Visual Studio 2010\AutomaTones3\automatones\AutomaTones\AutomaTones\Model\Tools\Math.cs</title></head><body><div class="banner"></div><h2 id="top">C:\Users\AO\Documents\Visual Studio 2010\AutomaTones3\automatones\AutomaTones\AutomaTones\Model\Tools\Math.cs</h2><div class="toggle"><p class="copyright"><a class="usercodeundertestcovered">user code under test</a>, 
  <span class="usercodeundertestnotcovered">user code under test (not covered)</span>, 
  <a class="userortestcodecovered">user code or test</a>, 
  <span class="userortestcodenotcovered">user code or test (not covered)</span>,
  <span class="tagged">tagged</span></p><table><tr><th>Methods</th></tr><tr><th /></tr></table><div class="toggle"><a class="button" href="#ID0EL">first</a><pre class="coverage">using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;

namespace AutomaTones.Model.Tools {
    /// &lt;author&gt;Anders Øland / anoe.itu.dk&lt;/author&gt;
    /// &lt;summary&gt;
    /// A few mathematical tool medthods.
    /// &lt;/summary&gt;
    public static class Math {
        /// &lt;summary&gt;
        /// Converts unsigned integer to any base.
        /// NOTE: result is given in an array of int's - so eg. hex numbers are represented by digits of 0-15
        /// &lt;/summary&gt;
        /// &lt;param name="intNum"&gt;&lt;/param&gt;
        /// &lt;param name="baseVal"&gt;&lt;/param&gt;
        /// &lt;param name="digits"&gt;specific number of digits for result (optional)&lt;/param&gt;
        /// &lt;returns&gt;&lt;/returns&gt;
        public static int[] DecimalToBase(uint intNum, uint baseVal, uint digits = 0u) <span id="ID0EL"><a class="button" href="#top">top</a>|<a class="button" href="#ID0EPC">next</a></span><a class="userortestcodecovered">{</a>
            Contract.Requires(0 &lt;= intNum &amp;&amp; 0 &lt; baseVal);
            Contract.Ensures(Contract.ForAll(Contract.Result&lt;int[]&gt;(), n =&gt; 0 &lt;= n &amp;&amp; baseVal &gt; n));
            Contract.Ensures(digits &lt;= Contract.Result&lt;int[]&gt;().Length);
            <a class="userortestcodecovered">return DecimalToBase((ulong) intNum, baseVal, digits);</a>
        <a class="userortestcodecovered">}</a>

        /// &lt;summary&gt;
        /// Converts unsigned long to any base.
        /// NOTE: result is given in an array of int's - so eg. hex numbers are represented by digits of 0-15
        /// &lt;/summary&gt;
        /// &lt;param name="longNum"&gt;&lt;/param&gt;
        /// &lt;param name="baseVal"&gt;&lt;/param&gt;
        /// &lt;param name="digits"&gt;specific number of digits for result (optional)&lt;/param&gt;
        /// &lt;returns&gt;&lt;/returns&gt;
        [Pure] // Tagged as pure since its being called from other contracts which I dont want to loosen 
        public static int[] DecimalToBase(ulong longNum, uint baseVal, uint digits = 0u) <a class="button" href="#ID0EL">prev</a>|<span id="ID0EPC"><a class="button" href="#top">top</a></span><a class="userortestcodecovered">{</a>
            Contract.Requires(0 &lt;= longNum &amp;&amp; 0 &lt; baseVal);
            Contract.Ensures(digits &lt;= Contract.Result&lt;int[]&gt;().Length);
            <a class="userortestcodecovered">var result = new LinkedList&lt;int&gt;();</a>
            <a class="userortestcodecovered">while (longNum &gt; 0)</a> <a class="userortestcodecovered">{</a>
                <a class="userortestcodecovered">result.AddLast((int) (longNum % baseVal));</a>
                <a class="userortestcodecovered">longNum /= baseVal;</a>
            <a class="userortestcodecovered">}</a>
            <a class="userortestcodecovered">while (digits &gt; result.Count)</a> <a class="userortestcodecovered">result.AddLast(0);</a>
            <a class="userortestcodecovered">return result.ToArray();</a>
        <a class="userortestcodecovered">}</a>

        /// &lt;summary&gt;
        /// Converts n digits of any base to decimal.
        /// &lt;/summary&gt;
        /// &lt;param name="baseNum"&gt;&lt;/param&gt;
        /// &lt;param name="baseVal"&gt;&lt;/param&gt;
        /// &lt;returns&gt;&lt;/returns&gt;
        [Pure] // Tagged as pure since its being called from other contracts which I dont want to loosen 
        public static uint BaseToDecimal(int[] baseNum, uint baseVal) {
            Contract.Requires(null != baseNum);
            Contract.Requires(0 &lt; baseNum.Length);
            Contract.Requires(0 &lt; baseVal);
            return (uint) baseNum.Select((t, i) =&gt; (t * (int) System.Math.Pow(baseVal, baseNum.Length - i - 1))).Sum();
        }
    }
}</pre></div></div><hr /><table width="100%"><tr><td valign="top"><span class="copyright">Copyright (c) Microsoft Corporation. All rights reserved.</span><br /><span class="button" onclick="copySourceToClipboard()">Copy full source to clipboard</span></td></tr></table><div id="debugdiv"> </div></body></html>